var app = getApp();
Page({
  data: {
    // tab切换
    currentTab: 0,
    apiurl: app.data.apiurl,
    openid: app.globalData.openid,
    myorders:''
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '我的订单'
    })
    /**
     * 获取系统信息
     */
  
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    // console.log(111);
    //我的订单
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/waitpay',
      data: { ostarts: 0, openid: app.globalData.openid},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log("lbh");
        console.log(res);
        that.setData({
          myorders: res.data
        });
      }
    });
  },
  /**
     * 滑动切换tab
     */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
    console.log(e.detail.current);
    //我的订单
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/waitpay',
      data: { ostarts: e.detail.current, openid: app.globalData.openid},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(e.detail.current);
        that.setData({
          myorders: res.data
        });
      }
    });
  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      //我的订单
      wx.request({
        url: getApp().data.apiurl + '/index.php/index/index/waitpay',
        data: { ostarts: e.target.dataset.current,openid: app.globalData.openid },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          // console.log(res );
          that.setData({
            myorders: res.data
          });
        }
      });
    }
  },
  //立即支付
  c: function (c) {
    var that = this
    // console.log(c);
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/selectgoodsorders',
      data: { oid: c.target.dataset.oid, openid: app.globalData.openid },
      header: {
        'content-type': 'application/json'
      },
      success: function (resa) {
        console.log(resa.data.data.oopenid);
        wx.request({
          url: app.data.apiurl + "/wxpay/wxpay.php?out_trade_no=" + resa.data.data['order_sn'] + '&total_fee=' + resa.data.data['opaymoney'] + '&openid=' + app.globalData.openid,
          data: {},
          method: 'POST',
          success: function (res) {
            console.log(res);
            console.log('unified order success, response is:', res)
            var payargs = res.data;
            wx.requestPayment({
              timeStamp: payargs.timeStamp,
              nonceStr: payargs.nonceStr,
              package: payargs.package,
              signType: payargs.signType,
              paySign: payargs.paySign,
              
            })
            
          },
        });
      }
    });
  },
  //取消订单
  b: function (b) {
    var that = this
    console.log(b);
    that.setData({
      oid: b.target.dataset.oid
    })
    wx.showModal({
      title: '提示',
      content: '是否取消订单',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: getApp().data.apiurl + '/index.php/index/index/delorders',
            data: { oid: b.target.dataset.oid, openid: app.globalData.openid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.request({
                url: getApp().data.apiurl + '/index.php/index/index/waitpay',
                data: { ostarts: 0, openid: app.globalData.openid },
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  console.log("lbh");
                  console.log(res);
                  that.setData({
                    myorders: res.data
                  });
                }
              });
            }
          });
        }
        console.log(src)
      }
    })

   
  },
  //确认收货
  a:function(a){
    var that=this
    // console.log(a);
    that.setData({
      oid: a.target.dataset.oid
    })
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/updstarts',
      data: { oid: a.target.dataset.oid, openid: app.globalData.openid},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
       wx.request({
          url: getApp().data.apiurl + '/index.php/index/index/waitpay',
          data: { ostarts: 0, openid: app.globalData.openid },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log("lbh");
            console.log(res);
            that.setData({
              myorders: res.data,
              currentTab:3
            });
          }
        });
      }
    });
  },

e:function(e){
  wx.switchTab({
    url: '../index/index'
  })
},



})